package com.resou.reader.bookstore.more;

import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.support.constraint.ConstraintLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.resou.reader.R;
import com.resou.reader.api.entry.LoginData;
import com.resou.reader.base.ResouBaseActivity;
import com.resou.reader.bookdetail.BookDetailActivity;
import com.resou.reader.data.bookstore.model.MorePlateInfo;
import com.resou.reader.mine.IView.UserInstance;
import com.resou.reader.utils.ToastUtil;

import java.util.List;

import butterknife.BindView;

public class MoreActivity extends ResouBaseActivity<MorePresenter> implements MoreContract.View, MoreAdapter.OnMoreAdapterInteractionListener {
    public static final int PAGE_NUM = 10;
    public static final String PLATE_NAME = "plate_name";
    public static final String PLATE_TYPE = "plate_type";

    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.progress_bar)
    LinearLayout progressBar;
    @BindView(R.id.net_error_container)
    RelativeLayout netErrorContainer;
    private MoreAdapter mAdapter;
    private String mPlateType;
    private String mGender;
    private SeeMoreAdapter mMoreAdapter;

    public static void start(Context context, String plateName, String plateType) {
        Intent starter = new Intent(context, MoreActivity.class);
        starter.putExtra(PLATE_NAME, plateName);
        starter.putExtra(PLATE_TYPE, plateType);
        context.startActivity(starter);
    }

    @Override
    protected void initStatusBar() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            getWindow().setStatusBarColor(getResources().getColor(R.color.colorAccent));
        }
        setSupportActionBar(toolbar);
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setDisplayHomeAsUpEnabled(true);
            actionBar.setDisplayShowTitleEnabled(false);
        }
    }

    @Override
    protected void initPresenter() {
        mPresenter = new MorePresenter(this);
    }

    boolean isLoadingMore = false;
    private static final String TAG = "MoreActivity";
    private int mCurrentPage = 0;

    @Override
    protected void initView() {

        String plateName = getIntent().getStringExtra(PLATE_NAME);
        tvTitle.setText(plateName);
        mPlateType = getIntent().getStringExtra(PLATE_TYPE);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(layoutManager);
//        mAdapter = new MoreAdapter();
        mMoreAdapter = new SeeMoreAdapter();
        recyclerView.setAdapter(mMoreAdapter);
//        mAdapter.setInteractionListener(this);
//        recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
//            @Override
//            public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
//                int lastVisibleItem = layoutManager.findLastVisibleItemPosition();
//                int totalItemCount = layoutManager.getItemCount();
//                if (lastVisibleItem >= totalItemCount - 2 && dy > 0) {  //还剩2个Item时加载更多
//                    if (!isLoadingMore) {
//                        mPresenter.loadNextData(mGender, mPlateType, PAGE_NUM);
//                        isLoadingMore = true;
//                    }
//                }
//            }
//        });
        mMoreAdapter.setOnLoadMoreListener(() -> {
            mPresenter.loadNextData(mGender, mPlateType, PAGE_NUM);
        }, recyclerView);
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_more;
    }

    @Override
    public void retryRefreshData() {

    }

    @Override
    public void initData() {
        LoginData loginData = UserInstance.getLoginData();
        mGender = "1";
        if (loginData != null) {
            mGender = loginData.getGender();
        }
        mPresenter.loadData(mGender, mPlateType, PAGE_NUM);
        netErrorContainer.setOnClickListener(v -> {
            mPresenter.loadData(mGender, mPlateType, PAGE_NUM);
        });
    }

    @Override
    public void showContent() {
        progressBar.setVisibility(View.GONE);
        netErrorContainer.setVisibility(View.GONE);
        recyclerView.setVisibility(View.VISIBLE);
    }

    @Override
    public void showProgress() {
        progressBar.setVisibility(View.VISIBLE);
        netErrorContainer.setVisibility(View.GONE);
        recyclerView.setVisibility(View.GONE);
    }

    @Override
    public void showError(Throwable throwable) {
        ToastUtil.makeShortToast("网络异常");
        progressBar.setVisibility(View.GONE);
        netErrorContainer.setVisibility(View.VISIBLE);
        recyclerView.setVisibility(View.GONE);
    }

    @Override
    public void showEmpty() {

    }

    @Override
    public void setData(List<MorePlateInfo> plateInfoList) {

    }

    @Override
    public void addData(List<MorePlateInfo> plateInfoList) {
//        mAdapter.addInfoList(plateInfoList);
//        isLoadingMore = false;
        mMoreAdapter.addData(plateInfoList);
        int page = mPresenter.getPageIndex();
        if (page == 9) {
            mMoreAdapter.loadMoreEnd();
        } else {
            mMoreAdapter.loadMoreComplete();
        }
    }

    @Override
    public void loadFailed() {
        mMoreAdapter.loadMoreFail();
    }

    @Override
    public void onItemClicked(String bookId) {
        BookDetailActivity.startDetailActivity(this, bookId);
    }

    class SeeMoreAdapter extends BaseQuickAdapter<MorePlateInfo, BaseViewHolder> {

        public SeeMoreAdapter() {
            super(R.layout.recycler_item_type_hot_search_item);
        }

        @Override
        protected void convert(BaseViewHolder helper, MorePlateInfo item) {
            helper.setText(R.id.tv_author, item.getNovelAuthor())
                    .setText(R.id.tv_book_intro, item.getNovelSummary())
                    .setText(R.id.tv_book_name, item.getNovelName())
//                    .setText(R.id.tv_tag1, item.getNovelCategoryName())
                    .setText(R.id.tv_tag2, item.getNovelCategoryName());
            helper.getView(R.id.tv_tag1).setVisibility(View.GONE);
            Glide.with(MoreActivity.this).load(item.getNovelCover()).into((ImageView) helper.getView(R.id.iv_cover));
            helper.itemView.setOnClickListener(v -> BookDetailActivity.startDetailActivity(MoreActivity.this, item.getNovelId()));
        }
    }
}
